import { Controller } from "../../mvc/core/Controller";
import { GuildUnionInviteListWindow } from "../view/guild/union/GuildUnionInviteListWindow";
import { Dispatcher } from "../../mvc/core/Dispatcher";
import { EventName } from "../mvc/EventName";
import { NetDispatcher } from "../../mvc/core/NetDispatcher";
import { EPublicCommand } from "../../../Message/Command/EPublicCommand";
import { DataEvent } from "../events/DataEvent";
import { MsgManager } from "../manager/MsgManager";
import { Language } from "../../../extend/language/Language";
import { SMiniGuildInfo } from "../../../Message/Game/SMiniGuildInfo";
import { Alert } from "../../../com/mui/controls/Alert";
import { GameProxy } from "../mvc/GameProxy";
import { GuildUnionPosition } from "../view/guild/union/rule/GuildUnionPosition";
import { GuildUnionInviteRenderInfo } from "../view/guild/union/GuildUnionInviteRenderInfo";
import { MessageBlock } from "../../../Framework/MQ/MessageBlock";
import { SMyGuildUnion } from "../../../Message/Game/SMyGuildUnion";
import { ModuleType } from "../view/common/ModuleType";
import { EGuildUnionStatus } from "../../../Message/Game/EGuildUnionStatus";
import { SGuildUnionApplyList } from "../../../Message/Game/SGuildUnionApplyList";
import { SGuildUnionApply } from "../../../Message/Game/SGuildUnionApply";
import { Guild } from "../../../ModuleType/Guild";
type int = number;
//class GuildUnionController
    
    export  class GuildUnionController extends Controller
    {
       

        public get inviteListWindow(): GuildUnionInviteListWindow
        {
            if (!this._inviteListWindow) 
            {
                this._inviteListWindow = new GuildUnionInviteListWindow();
            }
            return this._inviteListWindow;
        }

        protected /* override */ initServer(): void
        {
            Dispatcher.addEventListener(EventName.GuildUnionShowInviteList, this.onShowGuildUnionInviteListWindow);
            Dispatcher.addEventListener(EventName.GuildUnionLeave, this.doLeaveGuildUnion);
            Dispatcher.addEventListener(EventName.GuildUnionDispose, this.doDisposeGuildUnion);
            Dispatcher.addEventListener(EventName.GuildUnionCancelInvite, this.doCancelInviteGuildUnion);
            Dispatcher.addEventListener(EventName.GuildUnionGetGuildList, this.doGetGuildList);
            Dispatcher.addEventListener(EventName.GuildSearchSuccess, this.doGetGuildListSuccess);
            Dispatcher.addEventListener(EventName.GuildUnionDoInvite, this.doInviteGuild);
            Dispatcher.addEventListener(EventName.GuildUnionDoInviteSuccess, this.doInviteGuildSuccess);
            Dispatcher.addEventListener(EventName.GuildUnionDoAcceptInvite, this.doAcceptInvite);
            Dispatcher.addEventListener(EventName.GuildUnionDoRejectInvite, this.doRejectInvite);
            Dispatcher.addEventListener(EventName.GuildUnionDoAgreeSuccess, this.onDoAgreeSuccess);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicMyGuildUnion, this.onReceiveMyGuildUnionInfo);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicGuildUnionApplyList, this.onReveiveGuildUnionApplyList);
            return;
        }

        /* internal  */doInviteGuild(arg1: DataEvent): void
        {
            if (!cache.guild.isGuildLeader) 
            {
                MsgManager.showRollTipsMsg(Language.getString(60871));
                return;
            }
            var loc1=arg1.data["guild"] as SMiniGuildInfo;
            var loc2=arg1.data["allocate"] as int;
            var loc3=arg1.data["second"] as Boolean;
            var loc4=Language.getStringByParam(60876, loc2, loc1.guildName);
            Alert.extendObj = arg1.data;
            Alert.show(loc4, null, Alert.OK | Alert.CANCEL, null, this.onDoInviteGuildAlertHandler);
            return;
        }

        /* internal  */onDoInviteGuildAlertHandler(arg1: int, arg2: Object): void
        {
            var loc1=null;
            var loc2=0;
            var loc3=false;
            if (arg1 == Alert.OK) 
            {
                loc1 = arg2["guild"] as SMiniGuildInfo;
                loc2 = arg2["allocate"] as int;
                loc3 = arg2["second"] as Boolean;
                GameProxy.guildProxy.guildUnionApply(loc1.guildId, loc2, false, loc3);
            }
            return;
        }

        /* internal  */doInviteGuildSuccess(arg1: DataEvent): void
        {
            var loc1=arg1.data["cancel"];
            var loc2=arg1.data["second"];
            if (loc1 == false) 
            {
                if (this._inviteListWindow && !this._inviteListWindow.isHide) 
                {
                    if (loc2 && this._inviteListWindow.position == GuildUnionPosition.GuildUnionPositionTwo) 
                    {
                        this._inviteListWindow.hide();
                    }
                    else if (!loc2 && this._inviteListWindow.position == GuildUnionPosition.GuildUnionPositionThree) 
                    {
                        this._inviteListWindow.hide();
                    }
                }
            }
            return;
        }

        /* internal  */doAcceptInvite(arg1: DataEvent): void
        {
            if (!cache.guild.isGuildLeader) 
            {
                MsgManager.showRollTipsMsg(Language.getString(60871));
                return;
            }
            var loc1=arg1.data as GuildUnionInviteRenderInfo;
            var loc2=Language.getStringByParam(60877, loc1.allocate, loc1.guildName);
            Alert.extendObj = loc1;
            Alert.show(loc2, null, Alert.OK | Alert.CANCEL, null, this.onDoAcceptInviteAlertHandler);
            return;
        }

        /* internal  */onDoAcceptInviteAlertHandler(arg1: int, arg2: Object): void
        {
            var loc1=null;
            if (arg1 == Alert.OK) 
            {
                loc1 = arg2 as GuildUnionInviteRenderInfo;
                GameProxy.guildProxy.guildUnionAgree(loc1.guildId, false);
            }
            return;
        }

        /* internal  */doRejectInvite(arg1: DataEvent): void
        {
            if (!cache.guild.isGuildLeader) 
            {
                MsgManager.showRollTipsMsg(Language.getString(60871));
                return;
            }
            var loc1=arg1.data as GuildUnionInviteRenderInfo;
            GameProxy.guildProxy.guildUnionAgree(loc1.guildId, true);
            return;
        }

        /* internal  */onDoAgreeSuccess(arg1: DataEvent): void
        {
            var loc1=arg1.data["guildId"];
            var loc2=arg1.data["reject"];
            if (loc2 == false) 
            {
                if (this._inviteListWindow && !this._inviteListWindow.isHide) 
                {
                    this._inviteListWindow.hide();
                }
            }
            this.getInviteList();
            return;
        }

        /* internal  */onShowGuildUnionInviteListWindow(arg1: DataEvent): void
        {
            if (!cache.guildUnion.isInActive) 
            {
                MsgManager.showRollTipsMsg(Language.getString(60883));
                return;
            }
            var loc1=arg1.data as int;
            if (!cache.guide.guideApplyGuild) 
            {
                this.inviteListWindow.position = loc1;
                this.inviteListWindow.show();
            }
            if (loc1 != GuildUnionPosition.GuildUnionPositionOne) 
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.GuildUnionGetGuildList, {"guildName":"", "startIndex": 0}));
            }
            else 
            {
                this.getInviteList();
            }
            return;
        }

        /* internal  */getInviteList(): void
        {
            GameProxy.guildProxy.getGuildUnionApplyList();
            return;
        }

        /* internal  */doLeaveGuildUnion(arg1: DataEvent): void
        {
            if (!cache.guildUnion.isInActive) 
            {
                MsgManager.showRollTipsMsg(Language.getString(60883));
                return;
            }
            if (cache.guild.isGuildLeader) 
            {
                Alert.show(Language.getString(60872), null, Alert.OK | Alert.CANCEL, null, this.onLeaveGuildUnionAlertHandler);
            }
            else 
            {
                MsgManager.showRollTipsMsg(Language.getString(60871));
            }
            return;
        }

        /* internal  */onLeaveGuildUnionAlertHandler(arg1: int): void
        {
            if (arg1 == Alert.OK) 
            {
                GameProxy.guildProxy.guildUnionQuit();
            }
            return;
        }

        /* internal  */doDisposeGuildUnion(arg1: DataEvent): void
        {
            if (!cache.guildUnion.isInActive) 
            {
                MsgManager.showRollTipsMsg(Language.getString(60883));
                return;
            }
            if (cache.guild.isGuildLeader && cache.guildUnion.isMainGuild) 
            {
                Alert.show(Language.getString(60873), null, Alert.OK | Alert.CANCEL, null, this.onDisposeGuildUnionAlertHandler);
            }
            else 
            {
                MsgManager.showRollTipsMsg(Language.getString(60871));
            }
            return;
        }

        /* internal  */onDisposeGuildUnionAlertHandler(arg1: int): void
        {
            if (arg1 == Alert.OK) 
            {
                GameProxy.guildProxy.guildUnionQuit();
            }
            return;
        }

        /* internal  */doCancelInviteGuildUnion(arg1: DataEvent): void
        {
            if (!cache.guild.isGuildLeader) 
            {
                MsgManager.showRollTipsMsg(Language.getString(60871));
                return;
            }
            var loc1=arg1.data.guild as SMiniGuildInfo;
            if (loc1) 
            {
                Alert.extendObj = arg1.data;
                Alert.show(Language.getStringByParam(60874, loc1.guildName), null, Alert.OK | Alert.CANCEL, null, this.onCancelInviteGuildUnionAlertHandler);
            }
            return;
        }

        /* internal  */onCancelInviteGuildUnionAlertHandler(arg1: int, arg2: Object): void
        {
            var loc1=null;
            var loc2=0;
            if (arg1 == Alert.OK) 
            {
                loc1 = arg2.guild as SMiniGuildInfo;
                loc2 = arg2.allocation as int;
                GameProxy.guildProxy.guildUnionApply(loc1.guildId, loc2, true, cache.guildUnion.isSecondGuild(loc1.guildId));
            }
            return;
        }

        /* internal  */doGetGuildList(arg1: DataEvent): void
        {
            if (this._isGetting == false) 
            {
                GameProxy.guildProxy.searchGuilds(0, arg1.data["guildName"], arg1.data["startIndex"], false);
                this._isGetting = true;
            }
            return;
        }

        /* internal  */doGetGuildListSuccess(arg1: DataEvent): void
        {
            var loc1=null;
            var loc2=0;
            var loc3=0;
            var loc4=0;
            var loc5=0;
            var loc6=null;
            var loc7=null;
            var loc8=null;
            var loc9=0;
            if (this._isGetting == true) 
            {
                loc1 = arg1.data["guilds"] as Array<any>;
                loc2 = arg1.data["totalNum"] as int;
                loc3 = arg1.data["startIdx"] as int;
                loc4 = arg1.data["myRank"] as int;
                if (loc1.length != 0) 
                {
                    if (this._inviteListWindow && !this._inviteListWindow.isHide) 
                    {
                        if ((loc5 = this._inviteListWindow.position) != GuildUnionPosition.GuildUnionPositionOne) 
                        {
                            loc6 = [];
                            loc9 = 0;
                            while (loc9 < 6) 
                            {
                                if (loc1.length > loc9) 
                                {
                                    loc8 = loc1[loc9] as SMiniGuildInfo;
                                    (loc7 = new GuildUnionInviteRenderInfo(loc8)).allocate = cache.guildUnion.remainAllocate;
                                    loc7.allocateMax = cache.guildUnion.remainAllocate;
                                    loc7.position = loc5;
                                    loc7.isCanOperate = this.isCanOperate(loc8);
                                    loc6.push(loc7);
                                }
                                ++loc9;
                            }
                            this._inviteListWindow.updateGuildListDataprovider(loc6, loc3, loc2);
                        }
                    }
                }
                else 
                {
                    MsgManager.showRollTipsMsg(Language.getString(57342));
                }
                this._isGetting = false;
            }
            return;
        }

        /* internal  */isCanOperate(arg1: SMiniGuildInfo): Boolean
        {
            if (cache.guild.myGuildInfo) 
            {
                if (arg1.guildId == cache.guild.myGuildInfo.guildId) 
                {
                    return false;
                }
                if (arg1.hasUnion) 
                {
                    return false;
                }
                if (cache.guildUnion.myGuildUnionInfo) 
                {
                    if (cache.guildUnion.myGuildUnionInfo.firstGuildInfo && cache.guildUnion.myGuildUnionInfo.firstGuildInfo.guildId == arg1.guildId) 
                    {
                        return false;
                    }
                    if (cache.guildUnion.myGuildUnionInfo.secondGuildInfo && cache.guildUnion.myGuildUnionInfo.secondGuildInfo.guildId == arg1.guildId) 
                    {
                        return false;
                    }
                    if (cache.guildUnion.myGuildUnionInfo.thirdGuildInfo && cache.guildUnion.myGuildUnionInfo.thirdGuildInfo.guildId == arg1.guildId) 
                    {
                        return false;
                    }
                    return true;
                }
                return true;
            }
            return false;
        }

        /* internal  */onReceiveMyGuildUnionInfo(arg1: MessageBlock): void
        {
            var loc1=cache.guildUnion.hasUnion;
            var loc2=arg1.messageBase as SMyGuildUnion;
            cache.guildUnion.myGuildUnionInfo = loc2;
            var loc3;
            if (loc3 = cache.guildUnion.hasUnion) 
            {
                if (!loc1) 
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.HighlightGameUI, {"type":"Navbar", "name": Guild, "isGlow": false}));
                }
            }
            else if (loc2.secondStatus == EGuildUnionStatus._EGuildUnionStatusNo && loc2.thirdStatus == EGuildUnionStatus._EGuildUnionStatusNo) 
            {
                cache.guildUnion.myGuildUnionInfo.firstAllocation = 100;
            }
            Dispatcher.dispatchEvent(new DataEvent(EventName.GuildUnionInfoChange));
            return;
        }

        /* internal  */onReveiveGuildUnionApplyList(arg1: MessageBlock): void
        {
            var loc3=null;
            var loc4=null;
            var loc5=null;
            var loc6=0;
            var loc7=0;
            var loc1=arg1.messageBase as SGuildUnionApplyList;
            cache.guildUnion.guildUnionApplyList = loc1;
            var loc2=cache.guild.isGuildLeader;
            if (loc2 && loc1.guildUnionApplys.length > 0 && (!this._inviteListWindow || this._inviteListWindow.isHide)) 
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.HighlightGameUI, {"type":"Navbar", "name": Guild, "isGlow": true}));
            }
            else if (loc2) 
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.HighlightGameUI, {"type":"Navbar", "name": Guild, "isGlow": false}));
            }
            if (this._inviteListWindow && !this._inviteListWindow.isHide) 
            {
                if (this._inviteListWindow.position == GuildUnionPosition.GuildUnionPositionOne) 
                {
                    loc3 = [];
                    loc6 = loc1.guildUnionApplys.length;
                    loc7 = 0;
                    while (loc7 < loc6) 
                    {
                        loc5 = loc1.guildUnionApplys[loc7] as SGuildUnionApply;
                        (loc4 = new GuildUnionInviteRenderInfo(loc5.guildInfo)).allocate = loc5.allocation;
                        loc4.allocateMax = loc5.allocation;
                        loc4.position = this._inviteListWindow.position;
                        loc4.isCanOperate = loc2;
                        loc3.push(loc4);
                        ++loc7;
                    }
                    this._inviteListWindow.updateInvitedListDataprovider(loc3);
                }
            }
            Dispatcher.dispatchEvent(new DataEvent(EventName.GuildUnionAppleListChange));
            return;
        }

        private /* var */_inviteListWindow: GuildUnionInviteListWindow;

        private /* var */_isGetting: Boolean=false;
    }
